package grpc

import (
	"base_server/internal/models/diy_pattern"
	"base_server/internal/models/pattern"
	"context"

	"jihe.common/protos/base_server/proto"
)

func (s *Server) BaseCreatePattern(ctx context.Context, req *proto.BasePattern) (ret *proto.BaseNull, err error) {
	patternTbl := pattern.NewModel(s.Pgsql.Get())
	return patternTbl.Create(req)
}

func (s *Server) BaseUpdatePattern(ctx context.Context, req *proto.BasePattern) (ret *proto.BaseNull, err error) {
	patternTbl := pattern.NewModel(s.Pgsql.Get())
	return patternTbl.Update(req)
}

func (s *Server) BaseGetPatternDetail(ctx context.Context, req *proto.BasePattern) (ret *proto.BasePattern, err error) {
	patternTbl := pattern.NewModel(s.Pgsql.Get())
	return patternTbl.GetDetail(req.Id)
}

func (s *Server) BaseGetPatternList(ctx context.Context, req *proto.BaseGetPatternListReq) (ret *proto.BaseGetPatternListRes, err error) {
	patternTbl := pattern.NewModel(s.Pgsql.Get())
	req.Page--
	ret, err = patternTbl.GetList(req)
	if err != nil {
		return
	}
	ret.Page = req.Page + 1
	ret.PageSize = req.PageSize
	return
}

func (s *Server) BaseDeletePattern(ctx context.Context, req *proto.BasePattern) (ret *proto.BaseNull, err error) {
	ret = new(proto.BaseNull)
	patternTbl := pattern.NewModel(s.Pgsql.Get())
	err = patternTbl.Delete(req)
	return
}

func (s *Server) BaseDiyPatternCreate(ctx context.Context, req *proto.BaseDiyPattern) (ret *proto.BaseNull, err error) {
	diyPatternTbl := diy_pattern.NewModel(s.Pgsql.Get())
	return diyPatternTbl.Create(req)
}

func (s *Server) BaseDiyPatternList(ctx context.Context, req *proto.BaseDiyPattern) (ret *proto.BaseDiyPatternListRes, err error) {
	ret = new(proto.BaseDiyPatternListRes)
	diyPatternTbl := diy_pattern.NewModel(s.Pgsql.Get())
	ret.List, err = diyPatternTbl.GetList(req)
	return
}
